package com.example.paper.loginreg;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.LogUtils;
import com.example.db.DBManager;
import com.example.db.Goods;
import com.example.mvp.model.BaseEntity;
import com.example.mvp.view.BaseActivity;
import com.example.paper.R;
import com.example.paper.brean.HttpCode;
import com.example.paper.brean.LoginBean;
import com.example.paper.mvp.contract.LoginContract;
import com.example.paper.mvp.model.LoginModel;
import com.example.paper.mvp.presenter.LoginPresenter;

import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;

public class LoginActivity extends BaseActivity<LoginPresenter> implements LoginContract.LoginView {

    private LinearLayout vanish;
    private TextView accountCut;
    private LinearLayout vanish2;
    private TextView accountCut2;
    private ImageView backImg;
    //页面二的布局id
    private EditText etPhone2;
    private EditText etPwd2;
    private Button numberBt;
    //页面一布局id
    private EditText etPhone;
    private EditText etPwd;
    private Button phoneBt;


    @Override
    public View addStatusView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_login;
    }

    public void initView() {

        backImg = (ImageView) findViewById(R.id.back_img);
        etPhone2 = (EditText) findViewById(R.id.et_phone2);
        etPwd2 = (EditText) findViewById(R.id.et_pwd2);
        numberBt = (Button) findViewById(R.id.number_bt);
        etPhone = (EditText) findViewById(R.id.et_phone);
        etPwd = (EditText) findViewById(R.id.et_pwd);
        phoneBt = (Button) findViewById(R.id.phone_bt);
        vanish2 = (LinearLayout) findViewById(R.id.vanish2);
        accountCut2 = (TextView) findViewById(R.id.account_cut2);
        vanish = (LinearLayout) findViewById(R.id.vanish);
        accountCut = (TextView) findViewById(R.id.account_cut);

    }

    @Override
    public void initData() {

        mPresenter = new LoginPresenter(new LoginModel(), this);

        //登录
        numberBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = etPhone2.getText().toString().trim();
                //转型
                String pwd = etPwd2.getText().toString().trim();

                if (name.equals("")||pwd.equals("")){
                    Toast.makeText(LoginActivity.this, "用户名密码不能为空", Toast.LENGTH_SHORT).show();
                }else {
                    //传入参数
                    int names = Integer.parseInt(name);
                    //登陆成功
                    mPresenter.RequestLogin(names,pwd,HttpCode.MUSERLOGIN);

                }

            }
        });


        //注册
        phoneBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String name = etPhone.getText().toString().trim();
                //转型
                String pwd = etPwd.getText().toString().trim();
                if (name.equals("")||pwd.equals("")){
                    Toast.makeText(LoginActivity.this, "用户名密码不能为空", Toast.LENGTH_SHORT).show();
                }else {
                    if (name.length()==9){
                        //正则
//                        Pattern p = Pattern.compile("");
                        int names = Integer.parseInt(name);
                        //传入参数
                        mPresenter.RequestLogin(names,pwd, HttpCode.MUSERREG);

                    }else {
                        Toast.makeText(LoginActivity.this, "手机号类型不对", Toast.LENGTH_SHORT).show();
                    }
                }

            }
        });


        //加载布局
        getPop();

        getPop2();


        //退出当前页面
        backImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


    }


    //手机号密码登录
    public void getPop() {
        accountCut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AnimatorSet animatorSet = new AnimatorSet();
                ObjectAnimator translationY = ObjectAnimator.ofFloat(vanish, "translationY", 0, 200);
                ObjectAnimator alpha = ObjectAnimator.ofFloat(vanish, "alpha", 1, 0);
                animatorSet.setInterpolator(new LinearInterpolator());
                animatorSet.setDuration(1000);
                animatorSet.playTogether(translationY,alpha);
                animatorSet.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        super.onAnimationEnd(animation);

                        vanish.setVisibility(View.GONE);
                        vanish2.setVisibility(View.VISIBLE);

                        AnimatorSet animatorSet1 = new AnimatorSet();
                        ObjectAnimator alpha1 = ObjectAnimator.ofFloat(vanish2, "alpha", 0, 1);
                        ObjectAnimator translationY1 = ObjectAnimator.ofFloat(vanish2, "translationY", 200, 0);
                        animatorSet1.setInterpolator(new LinearInterpolator());
                        animatorSet1.setDuration(1000);
                        animatorSet1.playTogether(translationY1,alpha1);
                        animatorSet1.start();
                    }
                });
                animatorSet.start();
            }
        });
    }


    //手机号验证码登录
    public void getPop2() {
        accountCut2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AnimatorSet animatorSet = new AnimatorSet();
                ObjectAnimator translationY = ObjectAnimator.ofFloat(vanish2, "translationY", 0, 200);
                ObjectAnimator alpha = ObjectAnimator.ofFloat(vanish2, "alpha", 1, 0);
                animatorSet.setInterpolator(new LinearInterpolator());
                animatorSet.setDuration(1000);
                animatorSet.playTogether(translationY,alpha);
                animatorSet.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        super.onAnimationEnd(animation);

                        vanish2.setVisibility(View.GONE);
                        vanish.setVisibility(View.VISIBLE);
                        etPhone2.setText("");
                        etPwd2.setText("");

                        AnimatorSet animatorSet1 = new AnimatorSet();
                        ObjectAnimator alpha1 = ObjectAnimator.ofFloat(vanish, "alpha", 0, 1);
                        ObjectAnimator translationY1 = ObjectAnimator.ofFloat(vanish, "translationY", 200, 0);
                        animatorSet1.setInterpolator(new LinearInterpolator());
                        animatorSet1.setDuration(1000);
                        animatorSet1.playTogether(translationY1,alpha1);
                        animatorSet1.start();

                    }
                });
                animatorSet.start();
            }
        });
    }


    @Override
    public void refresh(BaseEntity baseEntity) {

        //强转
        LoginBean baseEntitytow = (LoginBean) baseEntity;
        Goods values = baseEntitytow.getValues();

        //成功进入
        if (baseEntity.getStatus().equals("200")&&baseEntity.message.equals("用户登录成功")){
            Log.e("============", "登录成功");
            Intent intent = new Intent(LoginActivity.this, ChangeActivity.class);
            startActivity(intent);

            //存入数据库
            DBManager.getDbManager().getDaoSession().insert(new Goods(values.getUser_id(),values.getUser_img()
                    ,values.getUser_name(),values.getUser_phone()
                    ,values.getUser_pwd(),values.getUser_sex(),true));

            //存入数据库查询
            List<Goods> goods = DBManager.getDbManager().getDaoSession().loadAll(Goods.class);
            LogUtils.json(goods);

            finish();
        }else if (baseEntity.getStatus().equals("200")&&baseEntity.message.equals("成功")){
            Log.e("============", "注册成功");

        }

    }

}